<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <template v-if="queryParams.type === '2'">
        <el-form-item label="代理昵称" prop="nickName" label-width="90px">
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入代理昵称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="代理账号" prop="userName" label-width="90px">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入代理账号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </template>
      <el-form-item label="银行卡名称" prop="bankName" label-width="90px">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入银行卡名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="提现金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入提现金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现时间" prop="withdrawalTime">
        <el-date-picker clearable
                        v-model="queryParams.withdrawalTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择提现时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="到账时间" prop="accountTime">
        <el-date-picker clearable
                        v-model="queryParams.accountTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择到账时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['withdrawal:withdrawalOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="withdrawalOrderList">
      <template v-if="queryParams.type === '2'">
        <el-table-column label="代理账号" align="center" prop="userName" min-width="150" show-overflow-tooltip/>
        <el-table-column label="代理昵称" align="center" prop="nickName" min-width="150" show-overflow-tooltip/>
      </template>
      <el-table-column label="类型" align="center" prop="type" min-width="100">
        <template slot-scope="{ row }">
          <el-tag v-if="row.accountType === 1" type="success">支付宝</el-tag>
          <el-tag v-else-if="row.accountType === 2" >银行卡</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="产品" align="center" prop="productName" min-width="100"/>
      <el-table-column label="银行卡名称" align="center" prop="bankName" min-width="150"/>
      <el-table-column label="账户类型 " align="center" prop="backType" min-width="100">
        <template slot-scope="{ row }">
          <el-tag v-if="row.backType === 1" type="success">个人</el-tag>
          <el-tag v-else-if="row.backType === 2" >对公</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus" min-width="130">
        <template slot-scope="{ row }">
          <el-tag style="cursor: pointer" type="info" v-if="row.auditStatus === 0">初始化</el-tag>
          <el-tag style="cursor: pointer" type="warning" v-else-if="row.auditStatus === 1">审核中</el-tag>
          <el-tag style="cursor: pointer" v-else-if="row.auditStatus === 2">审核通过</el-tag>
          <el-popover
            placement="top-start"
            :width="300"
            trigger="click"
            v-else-if="row.auditStatus === 3"
            :content="row.auditReason">
            <template #reference>
              <el-tag style="cursor: pointer" slot="reference" type="danger">审核驳回</el-tag>
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="提现状态" align="center" prop="status" min-width="130">
        <template slot-scope="{ row }">
          <el-tag style="cursor: pointer" type="info" v-if="row.status === 0">初始化</el-tag>
          <el-tag style="cursor: pointer" type="warning" v-else-if="row.status === 1">待提现</el-tag>
          <el-tag style="cursor: pointer" type="success" v-else-if="row.status === 2">提现中</el-tag>
          <el-tag style="cursor: pointer" v-else-if="row.status === 3">提现成功</el-tag>
          <el-popover
            placement="top-start"
            :width="300"
            trigger="click"
            v-else-if="row.status === 4||row.status === 5"
            :content="row.reason">
            <template #reference>
              <el-tag style="cursor: pointer" slot="reference" type="danger">{{ row.status === 4?"提现失败":row.status === 5?"提现回退":"" }}</el-tag>
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="提现金额" align="center" prop="amount" min-width="150">
        <template slot-scope="{ row }">
          到账金额：{{row.actualAmount}}
          <br>
          提现金额：{{row.amount}}
        </template>
      </el-table-column>
      <el-table-column label="手续费" align="center" prop="amount" min-width="130">
        <template slot-scope="{ row }">
          手续费：{{row.backFeeAmount}}
          <br>
          退还手续费：{{row.refundBackFeeAmount}}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="提现时间" align="center" prop="withdrawalTime" width="180" />
      <el-table-column label="到账时间" align="center" prop="accountTime" width="180"/>
      <template v-if="queryParams.type === '2'">
        <el-table-column label="操作" fixed="right" class-name="small-padding fixed-width" align="center" width="150">
          <template slot-scope="{ row }">
            <el-button
              size="mini"
              type="text"
              :disabled="row.auditStatus !== 0"
              icon="el-icon-check"
              v-hasPermi="['withdrawal:withdrawalOrder:toExamine']"
              @click="handleToExamine(row)"
            >审核</el-button>
          </template>
        </el-table-column>
      </template>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改提现订单对话框 -->
    <el-dialog title="提现审核" :visible.sync="open" width="500px" append-to-body destroy-on-close>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="代理名称" prop="tslName">
          {{ form.nickName }}
        </el-form-item>
        <el-form-item label="代理账号" prop="taskName">
          {{ form.userName }}
        </el-form-item>
        <el-form-item label="提现金额" prop="amount">
          {{ form.amount }}
        </el-form-item>
        <el-form-item label="税点" prop="taxPoint">
          <el-input
            v-model="form.taxPoint"
            placeholder="请输入税点"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-radio-group v-model="form.auditStatus">
            <el-radio
              v-for="dict in [{label:'审核成功',value:2},{label:'审核驳回',value:3}]"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="驳回原因" v-if="form.auditStatus === 3">
          <el-input v-model="form.reason" :autosize="{ minRows: 8, maxRows: 8}" type="textarea" placeholder="请输入驳回原因"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWithdrawalOrder } from '@/api/withdrawal/withdrawalOrder'
import store from '@/store'

export default {
  name: "WithdrawalOrder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 提现订单表格数据
      withdrawalOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tslId:null,
        productId: null,
        userId: null,
        type: this.$route.query.type,
        roleType: null,
        bankName: null,
        auditStatus: null,
        accountType: null,
        auditReason: null,
        reason: null,
        amount: null,
        withdrawalTime: null,
        status: null,
        accountTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        auditStatus:[
          { required: true, message: "请选择审核状态", trigger: ["blur","change"] },
        ],
      },
      roleGroup: store.getters.roles
    };
  },
  created() {
    if (this.roleGroup.includes('service_provider')) {
      let tslData = JSON.parse(localStorage.getItem('tslData') || '{}');
      if (tslData) {
        this.queryParams.tslId = tslData.id
      }
    }
    this.getList();
  },
  methods: {
    /** 查询提现订单列表 */
    getList() {
      this.loading = true;
      listWithdrawalOrder(this.queryParams).then(response => {
        this.withdrawalOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleToExamine(row){
      this.open = true
      this.form = {...row}

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            toExamineWithdrawalOrder(this.form).then(response => {
              this.$modal.msgSuccess("审核成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('withdrawal/withdrawalOrder/export', {
        ...this.queryParams
      }, `提现记录_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
